<template>
  <div class="login-wrap">
    <el-tabs v-model="activeName" stretch="true">
      <el-tab-pane label="登录" name="first">
        <el-form :model="userForm" :rules="rules" ref="userForm" label-width="0px">
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              prefix-icon="el-icon-search"
              autofocus="true"
              v-model="userForm.username">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-search"
              @keyup.enter.native="submitForm('userForm')"
              v-model="userForm.password"
              style="margin-top: 10px">
            </el-input>
          </el-form-item>
          <div class="login-btn">
            <el-button type="primary" @click="submitForm('userForm')">登录</el-button>
          </div>
      </el-form>
      </el-tab-pane>
      <el-tab-pane label="注册" name="second">
        <el-form :model="rigistForm" :rules="rules" ref="rigistForm" label-width="0px">
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              prefix-icon="el-icon-search"
              autofocus="true"
              v-model="rigistForm.username">
            </el-input>
          </el-form-item>
          <el-form-item prop="mobile">
            <el-input
              placeholder="请输入手机号码"
              prefix-icon="el-icon-search"
              v-model="rigistForm.mobile"
              style="margin-top: 10px">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-search"
              @keyup.enter.native="submitForm('rigistForm')"
              v-model="rigistForm.password"
              style="margin-top: 10px">
            </el-input>
          </el-form-item>
          <div class="login-btn">
            <el-button type="success" @click="submitForm('rigistForm')">注册</el-button>
          </div>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script type="text/ecmascript-6">
  import api from '~/plugins/api'
  export default {
    layout:'pure-layout',
    data: function () {
      return {
        activeName:'first',
        userForm: {
          username: '',
          password: ''
        },
        rigistForm:{
          username: '',
          password: '',
          mobile: ''
        },
        rules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      submitForm (formName, api) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            debugger
            this.api.auth.login(this.userForm).then(data => {
              if (data.success) {
                this.$router.push('/admin')
                this.$message({
                  message: '登录成功!',
                  type: 'success'
                })
              } else {
                this.$message({
                  message: '登录失败,' + data.msg,
                  type: 'error'
                })
              }
            })
          } else {
            this.$message({
              message: '登录失败',
              type: 'error'
            })
            return false
          }
        })
      }
    }
  }
</script>

<style scoped>
  .container {
    position: absolute;
    bottom: 0;
    top: 0;
    left: 0;
    right: 0;
    overflow: hidden;
    background-color: #f5f5f5;
  }

  .login-wrap {
    width: 400px;
    /*margin: 100px auto 0;*/
    background: #fff;
    border-radius: 5px;
    -webkit-border-radius: 3px;
    box-shadow: inset 0px 0px 1px 0px #6E6B6C;
    margin: 0 auto;
  }

  .title {
    margin: 0;
    padding: 25px 20px;
    text-align: center;
    background: #68dff0;
    border-radius: 5px 5px 0 0;
    -webkit-border-radius: 5px 5px 0 0;
    color: #fff;
    font-size: 20px;
    text-transform: uppercase;
    font-weight: 300;
  }

  .el-form {
    padding: 40px;
  }

  .login-btn button {
    width: 100%;
    border-radius: 20px;
  }
</style>
